package com.ai.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.sendreq.ICachUtil;
import com.ai.web.service.AppUserService;
import com.ai.web.vo.AppUserV;
import com.ai.web.vo.ICashPhoneReturnMsgV;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;
/**
 * App用户管理
* @ClassName:  
* @Description: TODO 
* @author:  
* @date: 2014年3月28日
*
 */
@Controller
@RequestMapping("/appUser")
public class AppUserController extends BaseController {
    
    
    private static Logger logger = Logger.getLogger(AppUserController.class);
    
    @Autowired
    private AppUserService appUserService;
    
    @RequestMapping(value = "/list")
    public String list(@RequestParam(defaultValue = "") String username,
            @RequestParam(defaultValue = "") String password) {
        
        logger.info("hello......test");
        return "appUser/appUser_list";
    }
    
    @RequestMapping(value = "/modifyPasswordTemplate")
    public String modifyPasswordTemplate(@RequestParam(defaultValue = "") String username,
            @RequestParam(defaultValue = "") String password) {
        
        logger.info("hello......test");
        return "appUser/modify-password";
    }
    
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        return appUserService.queryListAndCount(conditionMap);
    }
    
    @RequestMapping(value = "/new")
    public String newBgAppUser(Model model) {
        
        AppUserV appUserV = new AppUserV();
        model.addAttribute("appUserV", appUserV);
        
        return "appUser/appUser_template";
    }
    
    @RequestMapping(value = "/update/{userId}")
    public String updateBgAppUser(@PathVariable("userId") int userId, Model model)
            throws Exception {
        
        AppUserV appUserV = appUserService.getAppUser(userId);
        logger.info("----- getUser:" + appUserV);
        
        if (appUserV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute("appUserV", appUserV);
        
        return "appUser/appUser_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel saveBgAppUser(AppUserV AppUserV) throws Exception {
        
        logger.info("-----save:" + AppUserV);
        ResultModel rm = appUserService.saveAppUser(AppUserV);
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deleteBgAppUser(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = appUserService.deleteAppUsers(userIds);
        
        return rm;
    }
    
    @RequestMapping(value = "/regist", produces = "application/json")
    @ResponseBody
    public ResultModel registAppUser(@RequestParam String phone, 
    		@RequestParam long pushChannelId, 
    		@RequestParam String pushUserId,
    		@RequestParam int deviceType) throws Exception {
        
        logger.info("-----phone:" + phone + " pushChannelId:" 
        		+ pushChannelId + " pushUserId:" + pushUserId);
        
        ResultModel rm = new ResultModel();
        /**维护人员手机号码验证 */
        ICashPhoneReturnMsgV iCashPhoneReturnMsgV = ICachUtil.verificationPhone(phone);
        if(iCashPhoneReturnMsgV.getResult()==0) {
            //验证不通过
            rm.setFailCode();
            rm.setMsg(iCashPhoneReturnMsgV.getErrorMsg());
            return rm;
        }
        
        AppUserV appUserV = appUserService.getAppUserByPhone(phone);
        if (appUserV == null) {
        	appUserV = new AppUserV();
        	appUserV.setMobilePhone(phone);
        } 
        
        appUserV.setDeviceType(deviceType);
        appUserV.setPushChannelId(pushChannelId);
    	appUserV.setPushUserId(pushUserId);
    	appUserV.setUsername(iCashPhoneReturnMsgV.getName());
    	appUserV.setAddress(iCashPhoneReturnMsgV.getAddress());
    	appUserV.setCompany(iCashPhoneReturnMsgV.getCompany());
        
        rm = appUserService.saveAppUser(appUserV);

        return rm;
    }
    
}
